package com.alibaba.citrus.cr.mn.order.center.facade.repository.impl;

import com.alibaba.citrus.cr.common.annotation.RepositoryInvoker;
import com.alibaba.citrus.cr.mn.order.center.facade.repository.DealLineQueryRepository;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.crforecastsales.domian.deallinequeue.dto.LoadDealLineQueueListRequest;
import com.epoch.app.crforecastsales.domian.deallinequeue.dto.LoadListResponse;
import com.epoch.app.crforecastsales.domian.deallinequeue.model.DealLineQueue;
import com.epoch.app.crforecastsales.domian.deallinequeue.service.DealLineQueueService;
import com.epoch.app.crforecastsales.model.dto.DealLineQueueLoadListVO;
import com.epoch.app.crforecastsales.model.dto.DealLineSDO;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service("dealLineQueryRepositoryImplBiz")
public class DealLineQueryRepositoryImpl implements DealLineQueryRepository {

    private static Log log = Log.getLogger(DealLineQueryRepositoryImpl.class);

    @Resource
    DealLineQueueService dealLineQueueService;

    @Override
    @RepositoryInvoker
    public Result<List<DealLineQueue>> queryProgressQueueResult(DealLineSDO dealLineSDO) {
        log.info("DealLineQueryRepositoryImplBiz#queryProgressQueueResult dealLineSDO={}", dealLineSDO);

        boolean hasParams = false;
        LoadDealLineQueueListRequest dealLineQueueLoadListRequest = new LoadDealLineQueueListRequest();

        if (StringUtils.isNotBlank(dealLineSDO.getDealType())) {
            dealLineQueueLoadListRequest.setDealTypes(Lists.newArrayList(Integer.parseInt(dealLineSDO.getDealType())));
            hasParams = true;
        }

        if (StringUtils.isNotBlank(dealLineSDO.getForecastOrderLineId())) {
            dealLineQueueLoadListRequest.setForecastOrderLineId(dealLineSDO.getForecastOrderLineId());
            hasParams = true;
        }

        if (CollectionUtils.isNotEmpty(dealLineSDO.getForecastOrderLineIdList())) {
            dealLineQueueLoadListRequest.setForecastOrderLineIdList(dealLineSDO.getForecastOrderLineIdList());
            hasParams = true;
        }

        if (Objects.nonNull(dealLineSDO.getDealStatus())) {
            dealLineQueueLoadListRequest.setDealStatuses(Lists.newArrayList(dealLineSDO.getDealStatus()));
            hasParams = true;
        }

        if (StringUtils.isNotBlank(dealLineSDO.getCreator())) {
            dealLineQueueLoadListRequest.setCreator(dealLineSDO.getCreator());
            hasParams = true;
        }

        if (StringUtils.isNotBlank(dealLineSDO.getBatchNumber())) {
            dealLineQueueLoadListRequest.setBatchNumber(dealLineSDO.getBatchNumber());
            hasParams = true;
        }

        if (!hasParams) {
            return Result.success(Lists.newArrayList());
        }

        return dealLineQueueService.loadDealLineQueueList(dealLineQueueLoadListRequest);
    }

    @Override
    public List<Map> listDealLineQueue(String batchNo) {
        DealLineQueueLoadListVO dealLineQueueLoadListVO = new DealLineQueueLoadListVO();
        dealLineQueueLoadListVO.setBatchNumber(batchNo);
        LoadListResponse loadListResponse = dealLineQueueService.loadList(dealLineQueueLoadListVO);
        return loadListResponse.getResult();
    }


}
